package controllers.operset;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Zjlist;


import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class MyinfoController extends ApplicationController {
	public void index(){
		
	}
	
	public View getItems(int page, int rows, String q,Integer departmentId, String sort, String order) throws Exception {
		User user = (User) rbac.getCurrentUser();
		
		String orderBy = "id desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		if (departmentId != null){
			Zjlist department = Zjlist.find(Zjlist.class, departmentId);
			cond += " and departmentId in (" + department.findChildIds() + ")";
		}
		if (q != null){
			cond += " and (name like '%"+q+"%' or moNumber like '%"+q+"%' or homeNumber like '%"+q+"%')";
		}
		cond += " and id = " + user.getId();

		long total = User.count(User.class, cond, null);
		List<User> items = User.findAll(User.class, cond, null, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	

	
	public View update(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		User item = User.find(User.class, id);
		JsonView olditem= new JsonView(item);
		item = User.updateModel(item, params);
		if (item.save()){
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改个人资料，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	
	
	
}
